<?php
namespace app\backend\controller;
use app\backend\model\Config;
use app\common\model\Theme;

/**
 * @author Ray 541720500@qq.com
 */
class System extends Base{
    public function config()
    {
        if(request()->isPost()) {
            $this->saveConfig();
        }else {
            $model = model('Config');
            $data = $model->column('value','key');
            $this->assign('data', $model->makeFromData($data));
            $this->assign('action', 'Config');
            return $this->fetch('public/form_builder_page');
        }
	}
	public function saveConfig()
    {
        $data = input('post.');
        foreach ($data as $k => $v) {
            db('config')->where('key',$k)->update(['value' => $v]);
        }
        $this->makeLog('[修改系统设置]');
        $this->success('已保存');
    }
	public function upload()
    {
       return $this->uploadImage();
    }
    public function theme()
    {
        $where = [];

        $this->assign('data', [
            'title' => '留言反馈',
            'collection' => Theme::paginate(config('paginate.per_page')),
            'thead' => [
                ['#','width:60px'], '名称','状态'
            ],
            'fields' => [
                function($row) {
                    return '<image src="'.$row['image'].'" class="image-in-td">';
                }, 'name',
                function($row) {
                    return $row->status == 1 ? "已选用" : "";
                }
            ],
            'buttons' => [
                ['title'=>'选用','onclick'=>'ajaxReq','url'=> url('chooseTheme')],
            ],
        ]);
        return $this->fetch('public/table_builder');
    }
    public function chooseTheme()
    {
        Theme::where('id', '<>', input('id/d'))->update(['status' => 0]);
        $res = Theme::where('id', input('id/d'))->update(['status' => 1]);
        if ($res) {
            $this->success('启用成功');
        } else {
            $this->error('启用失败');
        }
    }
}